


set(protofiles "blog.proto" "user.proto" "user_action.proto")
set(odbfiles "Blog.hpp" "User.hpp")
set(odb_path ${CMAKE_SOURCE_DIR}/src/odb)
set(cur_path ${CMAKE_CURRENT_BINARY_DIR})
set(src_path ${CMAKE_CURRENT_LIST_DIR})
set(proto_path ${CMAKE_SOURCE_DIR}/src/protos)
set(compile_output_path ${CMAKE_SOURCE_DIR}/build/src)


# 生成protobuf文件
set(src_list "")
foreach(file  ${protofiles})
# compile protobuf file 
    set(hfile "")
    set(sfile "")
    string(REPLACE ".proto" ".pb.h" hfile ${file}) # blog.pb.h
    string(REPLACE ".proto" ".pb.cc" sfile ${file}) # blog.pb.cc
    if(NOT EXISTS ${compile_output_path}/${sfile}) 
        execute_process( # 这个指令是在cmake 。。执行的时候，就执行
            COMMAND protoc
                    -I ${proto_path} 
                    --cpp_out=${compile_output_path} 
                    ${proto_path}/${file}
        )
    endif()
    list(APPEND src_list ${compile_output_path}/${sfile})
endforeach()

# 生成odb文件
set(odb_file_list "")
foreach(file  ${odbfiles})
# compile protobuf file 
    set(hfile "")
    set(sfile "")
    string(REPLACE ".hpp" "-odb.hxx" hfile ${file}) # blog.pb.h
    string(REPLACE ".hpp" "-odb.cxx" sfile ${file}) # blog.pb.cc
    if(NOT EXISTS ${compile_output_path}/${sfile}) 
        execute_process( 
            COMMAND odb
                    -d mysql 
                    --std c++11 --generate-query --generate-schema --profile boost/date-time
                    --output-dir ${compile_output_path}
                    ${odb_path}/${file}
        )
    endif()
    list(APPEND odb_file_list ${compile_output_path}/${sfile})
endforeach()

file(GLOB SRC_FILES ${CMAKE_CURRENT_LIST_DIR}/*.cc)

add_executable(blogserver ${SRC_FILES} ${src_list} ${odb_file_list})
target_include_directories(blogserver PUBLIC ${compile_output_path} ${CMAKE_SOURCE_DIR}/src/user_module)

target_link_libraries(blogserver PUBLIC 
    brpc_etcd # etcd brpc
    leveldb -lodb -lodb-mysql # odb
    # redis
    -lhiredis
    -lredis++
     # es
    -ljsoncpp
)